package edu.tsinghua.ddb.dictionary;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.tsinghua.ddb.beans.Predicate;

public class Schema extends Identity implements IDatabase {
	private static final long serialVersionUID = 3393584990491629537L;

	public final Map<String, Table> tables = new HashMap<String, Table>();

	@Override
	public Boolean createTable(String tableName,
			List<Column> columnDefines) {
		if (tables.containsKey(tableName))
			return false;
		else {
			Table t = new Table(columnDefines);
			t.setName(tableName);
			tables.put(tableName, t);
			return true;
		}
	}

	@Override
	public Boolean dropTable(String tableName) {
		return tables.remove(tableName) != null;
	}

	@Override
	public Boolean hFragTable(String tableName, Map<Predicate, String> rules) {
		Table t = tables.get(tableName);
		if (t == null)
			return false;
		else {
			return t.hFrag(rules);
		}
	}

	@Override
	public Boolean vFragTable(String tableName, Map<Column, String> rules) {
		Table t = tables.get(tableName);
		if (t == null)
			return false;
		else {
			return t.vFrag(rules);
		}
	}

}

interface IDatabase {
	Boolean createTable(String tableName, List<Column> columnDefines);

	Boolean dropTable(String tableName);

	Boolean hFragTable(String tableName, Map<Predicate, String> rules);

	Boolean vFragTable(String tableName, Map<Column, String> rules);

}